package com.zut.fitness.service.impl;


import com.zut.fitness.common.Result;
import com.zut.fitness.pojo.FroUser;
import com.zut.fitness.pojo.dto.LoginDto;
import com.zut.fitness.service.IFroUserService;
import com.zut.fitness.mapper.FroUserMapper;
import com.zut.fitness.utils.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;


/**
 * 前台用户Service业务层处理
 * 
 * @author admin
 * @date 2025-01-02
 */
@Service
public class FroUserServiceImpl implements IFroUserService
{
    @Autowired
    private FroUserMapper froUserMapper;

    @Override
    public FroUser selectFroUserById(Long id) {
    return  froUserMapper.selectFroUserById(id);
    }

    @Override
    public Result login(LoginDto loginDto) {
        FroUser u=froUserMapper.findByUserName(loginDto.getUsername());
        if(u!=null){
            if(u.getPassword().equals(loginDto.getPassword())){
                Map<String,Object> claims=new HashMap<>();
                claims.put("id",u.getId());
                claims.put("username",u.getUsername());
                String token= JwtUtil.genToken(claims);
                return Result.success(token);
            }else{
                return Result.error(500,"密码错误");
            }
        }
        return Result.error(500,"用户不存在");
    }

    @Override
    public Result register(LoginDto loginDto) {
        FroUser u=froUserMapper.findByUserName(loginDto.getUsername());
        if(u==null){
            FroUser user=new FroUser();
            user.setUsername(loginDto.getUsername());
            user.setPassword(loginDto.getPassword());
            user.setNickname("用户"+loginDto.getUsername());
            user.setStatus("0");
            user.setCreateTime(new Date());
            user.setUpdateTime(new Date());
            froUserMapper.insertFroUser(user);
            return Result.success("注册成功");
        }
        return Result.error(500,"用户已存在");
    }





}
